<?php

namespace App\Admin\Controllers;

use App\Models\Panel;
use App\Models\Grade;
use App\Models\Semester;
use App\Models\Student;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;



class PanelController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('指标体系')
            ->description('评价小组')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('指标体系')
            ->description('评价小组详情')
            ->body($this->detail($id));

    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
        ->header('指标体系')
        ->description('编辑评价小组')
        ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('指标体系')
            ->description('新建评价小组')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Panel);

        $grid->id('Id')->sortable();
        $grid->column('semester.name', '学期');
        $grid->column('class.class_name', '班级');

        $grid->column('student.name','小组组长');
        $grid->column('students','小组成员')->display(function ($ids){
            $id_arr = explode(',',$ids);
//            dd($id);
            $students = Student::whereIn('id', $id_arr)->pluck('name')->implode('，');
            return $students;
        });


        $grid->actions(function ($actions) {
            $actions->disableView();
        });

        $grid->filter(function($filter){
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            // 在这里添加字段过滤器
            $filter->column(1/2, function ($filter) {
                $semesters = Semester::get();
                $semester_arr = [];

                foreach ($semesters as $semester) {
                    $semester_arr[$semester->id] = $semester->name;
                }
                $filter->equal('semester_id', '学期')->select($semester_arr);
            });
            $filter->column(1/2, function ($filter) {

                $grades = Grade::get();
                $grades_arr = [];

                foreach ($grades as $grade) {
                    $grades_arr[$grade->id] = $grade->class_name;
                }
                $filter->equal('class_id', '班级')->select($grades_arr);
            });
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Panel::findOrFail($id));


        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Panel());

        $semesters = Semester::get();
        $semester_arr = [];

        foreach ($semesters as $semester) {
            $semester_arr[$semester->id] = $semester->name;
        }

        $grades = Grade::get();
        $grades_arr = [];

        foreach ($grades as $grade) {
            $grades_arr[$grade->id] = $grade->class_name;
        }

        $students = Student::where('id','>',0)->get();
        $student_arr = [];

        foreach ($students as $student) {
            $student_arr[$student->id] = $student->name;
        }

        $form->select('semester_id','学期')->options($semester_arr)->setWidth(2,2);
        $form->select('class_id','班级')->options($grades_arr)->load('student_id', 'api/student')->setWidth(2, 2);
        $form->select('student_id','小组组长')->options(function ($student_id){
            $student_arr = [];
            $students = Student::where('id', $student_id)->first();
            if(!empty($students)) {
                $student_arr[$students->id] = $students->name;
            }

            return $student_arr;
        })->load('students', 'api/schoolmate')->setWidth(2,2);
        $form->multipleSelect('students','小组成员')->options(function ($ids){
            $students = [];
            if($ids) {
                $students = Student::whereIn('id', $ids)->pluck('name' , 'id');
            }
            return $students;
        })->setWidth(4,2)->help('小组成员与小组组长讨论同学综合素质，然后由组长负责录入评价成绩');

        $form->tools(function (Form\Tools $tools) {
            // 去掉`查看`按钮
            $tools->disableView();
        });
        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
        });

        return $form;
    }
}
